package com.edu.base.common.upload;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
@Service
public class LocalFileServiceImpl implements FileService {
	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Override
	public String singleFile(MultipartFile single) throws IOException {
		return saveSingleFile(single,null,false);
	}
   private String saveSingleFile(MultipartFile single,String appendPath,boolean bl){
	   HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String saveUrl = "";
		//1.判断是否上传
		if(!single.isEmpty()){
			String fileName = single.getOriginalFilename();
			String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
			// 文件保存路径  
			String savePath = null;
			
            savePath = request.getSession().getServletContext().getRealPath("/") + getPath(appendPath,bl,request);
            File existPath=new File(savePath);
            if(!existPath.exists())existPath.mkdirs();
           savePath=savePath	+ newFileName;
           logger.info("文件保存路径:" + savePath);
           saveUrl = request.getContextPath() +getPath(appendPath,bl,request)+ newFileName;
           logger.info("文件根路径:" + saveUrl);
           // 转存文件  
           try {
				single.transferTo(new File(savePath));
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		return saveUrl;
   }
   /**
    *  bl:false，在项目根路径下附加path路径, true:完全使用传入的路径
    * @param appendPath
    * @param bl
    * @param request
    * @return
    */
   private String getPath(String appendPath,boolean bl,  HttpServletRequest request ){
	   String savePath = "";
	   if(StringUtils.isEmpty(appendPath)){
		   savePath = "upload" +UploadFileUtils.separator ;
	   }else{
		   if(bl){
			   savePath=appendPath+UploadFileUtils.separator ;
		   }else{
			   savePath =  "upload" +UploadFileUtils.separator +appendPath+UploadFileUtils.separator;
		   }
	   }
	return savePath;
   }
	@Override
	public String[] multipleFile(MultipartFile[] multiple) throws IOException {
		List<String> list = new ArrayList<String>();
		String saveUrl = "";
		for (MultipartFile single : multiple) {
			saveUrl = singleFile(single);
			if(StringUtils.isNotEmpty(saveUrl)) {
				list.add(saveUrl);
			}
			saveUrl.trim();
		}
		String [] array = list.toArray(new String[list.size()]);
		return array;
	}

	@Override
	public String[] singleVideoFile(MultipartFile single) throws IOException {
		String [] array = new String[2];
		array [0] = singleFile(single);
		array [1] = screenImg(single);
		return array;
	}
	/**
	 * 截取视频缩略图
	 * @param single
	 * @return
	 */
	private String screenImg (MultipartFile single){
		return "";
	}
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 10; i++) {
			list.add(String.valueOf(i));
		}
		/*String [] array = (String[]) list.toArray(new String[list.size()]);
		System.out.println(array[2]);*/
		int[] fun ={0,1,2,3,4,5,6}; 
		System.arraycopy(fun,5,fun,0,2);
		for (int i = 0; i < fun.length; i++) {
			System.out.println(fun[i]);
		}
	}

	@Override
	public String singleFile(MultipartFile single, String path, boolean bl)
			throws IOException {
		return saveSingleFile(single,path,bl);
	}

	@Override
	public String[] multipleFile(MultipartFile[] multiple, String path,
			boolean bl) throws IOException {
		List<String> list = new ArrayList<String>();
		String saveUrl = "";
		for (MultipartFile single : multiple) {
			saveUrl = singleFile(single,path,bl);
			if(StringUtils.isNotEmpty(saveUrl)) {
				list.add(saveUrl);
			}
			saveUrl.trim();
		}
		String [] array = list.toArray(new String[list.size()]);
		return array;
	}
}
